<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="./jq-3.6.0.js"></script>
    <style>
        .header {
            width: 500px;
            height: 50px;
            background-color: red;
            display: flex;
            border: 1px solid black;
            text-align: center;
            line-height: 50px;
        }
        
        .header>div {
            border-right: 1px solid black;
            flex: 1;
        }
        
        .main {
            width: 500px;
            text-align: center;
            line-height: 50px;
        }
        
        .item {
            border: 1px solid black;
            display: flex;
        }
        
        .item>div {
            height: 50px;
            border-right: 1px solid black;
            flex: 1;
        }
    </style>
</head>

<body>
    <div>
        书名:<input id="name" /> <button onclick="query()">查询</button>
    </div>
    <div>
        <button onclick="toAdd()">添加</button>
    </div>
    <div class="header">
        <div>序号</div>
        <div>书名</div>
        <div>作者</div>
        <div>价格</div>
        <div>操作</div>
    </div>
    <div class="main">

    </div>
</body>
<script>
    const queryBookList = (name1) => {
        $.post('http://localhost:8082/books/query', {
            name: name1
        }, res => {
            if (res.code == '2000') {
                alert(res.msg)
                $.each(res.data, (i, e) => {
                    const itemDiv = $(`<div class="item">
            							<div>${e.id}</div>
            							<div>${e.name}</div>
            							<div>${e.author}</div>
            							<div>${e.price}</div>
            							<div>
                                            <span class="del">删除</span>
                                            <span class="update">修改</span>
                                        </div>
        						</div>`);
                    itemDiv.find('.del').on('click', function(e) {
                        var id = $(e.target).parent().siblings().get(0).innerHTML
                        $.get('http://localhost:8082/books/delById', {
                            id: id
                        }, res => {
                            console.log(res);
                            if (res.code == '2000') {
                                alert(res.msg)
                                query();
                            } else {
                                alert(res.msg)
                            }

                        })
                    })
                    itemDiv.find('.update').on('click', e => {
                        var id = $(e.target).parent().siblings().get(0).innerHTML
                        window.location.href = 'update.html?id=' + id
                    })
                    $('.main').append(itemDiv);
                    // console.log(i, $('.update').on);

                })
            }

        })
    }
    queryBookList();
    const query = () => {
        $('.main').html('')
        let name = $('#name').val();
        queryBookList(name);
    }
    const toAdd = () => {
        window.location.href = 'add.html'
    }
</script>

</html>